Local
Local <.varname.>
 
Parameters: NONE
Returns: NONE
 
     Local declarations like Global declarations, affect the visibility of variables. By default, when a variable is used without being previously declared as Local, Global or Static, PlayBASIC will automatically make the variable local to the scope it was declared within. While It's considered good programming practice to always declare your variables, it's not a requirement.

      Thus variables used within the program main, are local to the main program and are not visible from within your Functions & Psub's. Likewise, any variables created within your functions / subs will also only be visible within the Function / Psub (the scope) they were used within. This is often referred to as encapsulation, as variables will only exist within the scope their defined and are not accessible program wide. This protects your variables from being harmed by a different scope accidentally.

      However while variables default to local scope automatically, without manual declaration, there are occasions when you will need manually force a variable to local. To ensure that PlayBASIC uses the correct local declaration of a particular variable name (within a function), over a global variable that shares the same name.

      This occurs because when the compiler sees a variable within a function or sub, it first checks for any global instances of this variable name, if one exists, the global instance is the one that's used. If not, it defaults to local. So if the situation arises that you wish to use a variable name within a function that is also declared as global (in some other part of your program), you can override this with the Local directive.

     Local supports two basic forms, You can simply define a single local or list of locals using comas to separate them, or define a local with an assignment.
i.e.

  
  Local MyVariable
  
  Local Monday,Tuesday,Wednesday,Thursday,Friday
  
  Local Score = 1000
  


      Note: Variables defined as local will have precedence over any variable of the same name, that was defined in main program as global.



FACTS:


      * Local variables can only be defined within Functions

     * Local can handle assignments. I.e. (Local Score=1000)

      * Local can handle lists by using coma's between them. I.e. (Local Monday,Tuesay,Wednesday,Thursday,Friday)

      * Local Variables have precedence over any variable of the same name that was defined in main program as global.


Mini Tutorial :

     This simple examples is meant to shows how Local declarations can override external global variables declarations.

  
; Define a GLOBAL variable and assign it a value
  Global MyVariable = 123456
  
; Call a function to see if this global variable
; can be seen from within this function
  Show_Global_Visible()
  
; Call a function to show that functions can override
; globals using locals if the users wants
  Show_Local_Visible()
  
  
; Display our global variable from with the same
; scope
  Print "Main Scope:"+Str$(MyVariable)
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  
  
; ==========================================================
; Define a User Defined Function called Show_Global_Variable
; ==========================================================
  
Function Show_Global_Visible()
  
; Because MyVariable was previously defined as
; global in the main program.
  
; The compiler will use the global version of
; this variable, rather than create a local
; instance of the same name.
  
; So here were printing out whatever value the global
; version has within it
  
  Print "Global MyVariable from within function:"+Str$(MyVariable)
  
EndFunction
  
  
; ==========================================================
; Define a User Defined Function called Show_Global_Variable
; ==========================================================
  
Function Show_Local_Visible()
  
; Declare a local variable of name MyVariable,
; this local instance now takes precedence
; over any previously defined globals, and is
; completely separate variable
  
  Local MyVariable
  
; Set the LOCAL instance of MyVariable to 66
  MyVariable= 66
  
; Display this Local instance to the screen.
  Print "Local MyVariable from within function:"+Str$(MyVariable)
  
EndFunction
  


      In the output, You'll notice that he global variable is only visible from within the Show_Global_Visible function.

      While in the Show_Local_Visible() function the code is creating a local variable of the same name and showing it.

      Functions/Psub have their own sets of variables completely, so while the MyVaribale variable name is used within the main program and the function, they are two entirely separate variables. Who share a name, but nothing else.


  
  Global MyVariable from within Function123456
  Local MyVariable from within Function:66
  Main Scope:123456
  




 
Related Info: Constant | Dim | Explicit | Function | Functions&Psub | Global | Psub | Static | Variables :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com